%matplotlib inline
## Required libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib import animation, rc
from IPython.display import HTML
## change the values 2, 2 as desired, it represents the number of rows and columns
fig, ax = plt.subplots(2, 2, figsize=(11, 11))
## cange dpi value to inc/dec the quality of the video
fig.dpi = 80
df = pd.read_csv("Data/data.csv")
## The plot on the top left has the index [0,0], the index then follows that of a common matrix
line1, = ax[0,0].plot([], [], color="red")
line2, = ax[0,1].plot([], [], color="green")
line3, = ax[1,0].plot([], [], color="orange")
line4, = ax[1,1].plot([], [], color="blue")
line = [line1, line2, line3, line4]
# initialization function: plot the background of each frame
def init():
for l in line:
l.set_data([], [])
return line
# animation function. This is called sequentially
def animate(i):
interval = 100
i *=interval
for j in range(1,5):
x = df.iloc[i:(i+interval),0].values
y = df.iloc[i:(i+interval),j].values
idx = 1
if j <= 2:
idx = 0
ax[idx,(j+1)%2].set_xlim(i,i+interval)
ax[idx,(j+1)%2].set_ylim((df.iloc[:,j].min(), df.iloc[:,j].max()))
line[j-1].set_data(x,y)
return line
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=426, interval=1000, blit=True)
HTML(anim.to_html5_video())